package com.xszx.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.xszx.bean.LoginUser;

/**
 * 登录拦截器
 * @author renlihuang
 *记得在启动类中扫包
 */
@Component
public class LoginInterceptor implements HandlerInterceptor{
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
	
	   boolean flag=false;
	   LoginUser loginUser=(LoginUser)request.getSession().getAttribute("loginUser");
	   if(loginUser==null) {
		   request.setAttribute("msg", "用户未登录");
		  response.sendRedirect("/toLogin");
		//  PrintWriter writer=response.getWriter();

		   return flag; 
	   }else {
		   flag=true;		  
		   return flag;
	   }
		
		
    }

}
